//
//  LoadingIndicator.m
//  VietNews
//
//  Created by Nguyen .a.Hph on 8/4/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "LoadingIndicator.h"
#import <QuartzCore/QuartzCore.h>

@implementation LoadingIndicator

@synthesize labelMessage, activityIndicator;

- (id)init {    
    //if (self = [super initWithFrame:CGRectMake(25, 130, 270, 100)]) {
    if (self = [super initWithFrame:CGRectMake(100, 10, 100, 50)]) {
        // Vue
        self.backgroundColor = [UIColor clearColor];
        self.alpha = 0.80;
        self.layer.cornerRadius = 5;
        
        // Label : message
        //UILabel *theLabelMessage = [[UILabel alloc] initWithFrame:CGRectMake(15, 65, 240, 20)];
        UILabel *theLabelMessage = [[UILabel alloc] initWithFrame:CGRectMake(35, 5, 60, 40)];
        theLabelMessage.backgroundColor = [UIColor clearColor];
        theLabelMessage.textColor = [UIColor grayColor];
        theLabelMessage.text = NSLocalizedString(@"Loading...", @"Loading...");
        theLabelMessage.textAlignment = UITextAlignmentCenter;
        theLabelMessage.font = [UIFont boldSystemFontOfSize:16];
        theLabelMessage.adjustsFontSizeToFitWidth = YES;
        
        self.labelMessage = theLabelMessage;
        [self addSubview:theLabelMessage];
        
        // Activity Indicator
        UIActivityIndicatorView *theActivityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        //theActivityIndicator.frame = CGRectMake(115, 15, 40, 40);
        //theActivityIndicator.frame = CGRectMake(115, 15, 40, 40);
        theActivityIndicator.center = CGPointMake(15, 25);
        
        self.activityIndicator = theActivityIndicator;
        [self addSubview:theActivityIndicator];
        
    }
    return self;
}

- (void)show:(NSString *)theMessage {
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    self.labelMessage.text = theMessage;
    [self.activityIndicator startAnimating];
    self.hidden = NO;
    [self.superview bringSubviewToFront:self];
    [self refreshPosition];
}

- (void)hide {
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    [self.activityIndicator stopAnimating];
    self.hidden = YES;
}

- (void)refreshPosition {
    self.center = self.superview.center;
}

- (void)dealloc {
    self.labelMessage = nil;
    self.activityIndicator = nil;
}

@end
